package com.aurora.app.service;

import java.util.List;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.stereotype.Service;

import com.aurora.app.dao.AssessmentDao;
import com.aurora.app.dao.MessageDao;
import com.aurora.app.entity.Assessment;
import com.aurora.app.entity.Message;
import com.aurora.app.enums.AssessmentType;
import com.aurora.app.enums.FakeDelete;
import com.aurora.app.enums.MessageType;
import com.aurora.base.dao.BaseDao;
import com.aurora.base.service.BaseService;
@Service
public class MessageService extends BaseService<Message> {
	@Resource
	private MessageDao baseDao;
	
	@Override
	public BaseDao<Message> getBaseDao() {
		// TODO 自动生成的方法存根
		return this.baseDao;
	}
	
	public List<Message> getUserMessageListNotRead(String userId){
		return this.baseDao.list("o.isDelete = ? and o.toUser.id = ? and o.isRead = ?", FakeDelete.NODELETE,userId,false);
	}
	public List<Message> getUserMessageList(String userId){
		return this.baseDao.list("o.isDelete = ? and o.toUser.id = ?", FakeDelete.NODELETE,userId);
	}
	public Long getUserMessageCountNotRead(String userId){
		return this.baseDao.getCount("o.isDelete = ? and o.toUser.id = ? and o.isRead = ?", FakeDelete.NODELETE,userId,false);
	}
	
	/**
	 * 检测一则聊天类的消息是否应该添加
	 * 添加规则：来自同一个聊天组，上一条的消息，用户没有读的话，就应该放回false
	 * @param userId
	 * @param groupId
	 * @return
	 */
	public Boolean checkTheChatTypeMsgShouldAdd(String userId,String groupId){
		Long count = this.baseDao.getCount("o.isDelete = ?  and o.messageType = ? and o.toUser.id = ? and o.tip like ? and o.isRead = 0", FakeDelete.NODELETE,MessageType.Chat,userId,"%"+groupId+"%");
		System.out.println("count"+count);
		if(count > 0) return false;
		else return true;
	}
	
	/**
	 * 已读消息
	 * @param msgId
	 */
	@Transactional
	public void readMessage(Object...msgId){
		for(Object id : msgId){
			this.baseDao.updateProperty("isRead", true, id.toString());
		}
	}
	
}
